﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using INSTA.StockApplication.BLL;

namespace INSTA.StockApplication.UI.Windows
{
    public partial class Commandes : Form
    {
        DataGridViewTextBoxColumn tQuantite = new DataGridViewTextBoxColumn();
        int _indexDataGrid;
        bool productsAdded = false;

        public Commandes()
        {
            InitializeComponent();
            //dataGridView1.DataSource = commandes.GetListOfProductsFromDatabase();
            RefreshDatas();
            tQuantite.Name = "QUANTITE";
            dataGridView1.Columns.Add(tQuantite);
            FillComboBox();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows[_indexDataGrid].Cells["QUANTITE"].Value.ToString() == "")
            {
                MessageBox.Show("Veuillez renseigner une quantité");
            }

            else
            {
                if(CheckQuantiteSaisie(dataGridView1.Rows[_indexDataGrid].Cells["QUANTITE"].Value.ToString()))
                {
                    if (CheckQuantityFromStock(Convert.ToInt32(dataGridView1.Rows[_indexDataGrid].Cells["QUANTITE"].Value.ToString()),
                    Convert.ToInt32(dataGridView1.Rows[_indexDataGrid].Cells["QUANTITE_RESTANTE"].Value.ToString())))
                    {
                        if (!IsProductAlreadyAdded(Convert.ToInt32(dataGridView1.Rows[_indexDataGrid].Cells["ID_PRODUIT"].Value.ToString())))
                        {
                            dataGridView2.Rows.Add(dataGridView1.Rows[_indexDataGrid].Cells["ID_PRODUIT"].Value.ToString()
                                , dataGridView1.Rows[_indexDataGrid].Cells["NOM"].Value.ToString()
                                , dataGridView1.Rows[_indexDataGrid].Cells["QUANTITE"].Value.ToString()
                                , dataGridView1.Rows[_indexDataGrid].Cells["PRIX_VENTE"].Value.ToString()
                                , Convert.ToInt32(dataGridView1.Rows[_indexDataGrid].Cells["PRIX_VENTE"].Value.ToString()) * Convert.ToInt32(dataGridView1.Rows[_indexDataGrid].Cells["QUANTITE"].Value.ToString()));
                            productsAdded = true;
                            //Update column quantité dataGridView1

                        }
                        else
                        {
                            MessageBox.Show("Produit deja ajouté");
                        }

                    }
                    else
                    {
                        MessageBox.Show("Quantité insuffisante en stock");
                    }
                }
                else
                {
                    MessageBox.Show("Veuillez renseigner un entier");
                }    
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _indexDataGrid = e.RowIndex;
        }

        /// <summary>
        /// REMPLIT LA COMBO BOX AVEC LES CLIENTS
        /// </summary>
        private void FillComboBox()
        {
            //comboBox1.DataSource  = commandes.GetListOfClientsFromDatabase();
            comboBox1.ValueMember = "ID";
            comboBox1.DisplayMember = "NOM";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            
        }

        private bool CheckQuantiteSaisie(string qte)
        {
            int tampon = 0;
            bool result = int.TryParse(qte,out tampon);
            return result;
        }

        private bool CheckQuantityFromStock(int qteSaisie,int qteStock)
        {
            return qteSaisie <= qteStock;
        }

        private bool IsProductAlreadyAdded(int id)
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                if(Convert.ToInt32(row.Cells["id"].Value) == id)
                {
                    return true;
                }
            }
            return false;
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnValiderCommande_Click(object sender, EventArgs e)
        {
            if(productsAdded)
            {
                foreach (DataGridViewRow row in dataGridView2.Rows)
                {
                    if(row.Index == dataGridView2.RowCount-1)
                    {
                        MessageBox.Show("Commande enregistrée");
                        ResetFields();
                        RefreshDatas();
                        break;
                    }
                    else
                    {
                        int client = Convert.ToInt32(comboBox1.SelectedValue.ToString());
                        int produit = Convert.ToInt32(row.Cells["ID"].Value);
                        int quantite = Convert.ToInt32(row.Cells["QUANTITE"].Value);

                        /*if (!commandes.AddOrderIntoDatabase(client,DateTime.Today))
                        {
                            MessageBox.Show("Une erreur s'est produite lors de l'ajout de la commande");
                            break;
                        }*/
                    }
                }
            }
            else
            {
                MessageBox.Show("Veuillez ajouter des produits");
            }
        }

        private void ResetFields()
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells["QUANTITE"].Value = "";
            }
            dataGridView2.Rows.Clear();
        }

        private void RefreshDatas()
        {
            BindingSource bs = new BindingSource();
            //bs.DataSource = commandes.GetListOfProductsFromDatabase();
            dataGridView1.DataSource = bs;
            bs.ResetBindings(false);
        }

        private void Commandes_Load(object sender, EventArgs e)
        {

        }
    }
}
